package com.mg.framework.kernel;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public abstract class ActorComponent extends BaseActor {

	protected volatile int width = 0;
	protected volatile int height = 0;
	protected Bitmap bitmap = null;
	protected Canvas canvas = null;
	protected ComplexActor complexActor = null;

	public ActorComponent(ComplexActor complexActor, Bitmap bitmap) {
		super();
		this.bitmap = bitmap;
		this.init(complexActor);
	}

	public ActorComponent(ComplexActor complexActor, int width, int height) {
		super();
		this.bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		this.init(complexActor);
	}

	private void init(ComplexActor complexActor) {
		this.complexActor = complexActor;
		this.width = this.bitmap.getWidth();
		this.height = this.bitmap.getHeight();
		this.canvas = new Canvas(this.bitmap);
	}

	protected void drawSelf(Canvas canvas, Paint newPaint) {
		super.drawSelf();
		if (newPaint == null) {
			canvas.drawBitmap(this.bitmap, this.matrix, this.paint);
			return;
		}
		canvas.drawBitmap(this.bitmap, this.matrix, newPaint);
	}

	protected void flipHorizontally() {
		this.bitmap = this.getNewFlipHorizontallyBitmap();
		this.canvas = new Canvas(this.bitmap);

	}

	protected void flipVertically() {
		this.bitmap = this.getNewFlipVerticallyBitmap();
		this.canvas = new Canvas(this.bitmap);
	}

	private Bitmap getNewFlipHorizontallyBitmap() {
		Bitmap newBitmap = Bitmap.createBitmap(this.bitmap, 0, 0, this.width,
				this.height, flipHorizontally(this.width, this.height), true);
		return newBitmap;
	}

	private Bitmap getNewFlipVerticallyBitmap() {
		Bitmap newBitmap = Bitmap.createBitmap(this.bitmap, 0, 0, this.width,
				this.height, flipVertically(this.width, this.height), true);
		return newBitmap;
	}

	public abstract void gameUpdate();

	public abstract void screenUpdate(Canvas canvas);
}